
#include "CBaseScene.h"
#include "gameEngine.h"
namespace game
{
	
	CBaseScene::CBaseScene(scene::IGameSceneManager* gameSceneManager):	Established(false),
																		GameSceneManager(0)
	{
		 this->GameSceneManager = gameSceneManager;
	}

	CBaseScene::CBaseScene()
	{
		//not used
	}

	CBaseScene::~CBaseScene()
	{

	}

	void CBaseScene::establishScene()
	{
		establishObjects();

		this->Established = true;
	}

	void CBaseScene::establishObjects()
	{
		irr::core::list<scene::IDynamicSceneObject*>::Iterator it = DynamicObjects.getLast();
		
		game::getDeviceManager()->lockVideo();
		
		for(u32 i = DynamicObjects.getSize();i>0;i--)
		{
			(*it)->addToScene();
			//printf("\n\nadd to scene");
			it--;
		}

		game::getDeviceManager()->unlockVideo();
	}


	void CBaseScene::onNetworkManagerDisconneted()
	{

	}

	bool CBaseScene::onNetworkRequest()
	{
		return false;
	}

	bool CBaseScene::onUserAction(SUserAction action)
	{
		bool result = false;

		switch(action.KeyCode)
		{
		case irr::KEY_MOUSE:
			{
				if(action.KeyState == KEY_STATE_MOUSE_MOVE)
				{
					//printf("\nangle %d \n", action.MouseMovingInfo.getAngle());

					//printf("\nlength %f \n", action.MouseMovingInfo.getLength());

					//printf("\nX %d \n", action.MouseMovingInfo.X);
					//printf("\nY %d \n", action.MouseMovingInfo.Y);

					result = true;
				}
				else if (action.KeyState == KEY_STATE_MOUSE_STOPPED)
				{
					result = true;
					//printf("\nCBASESTOP");
				}
				else
				{
					//printf("\nCBASE MOUSE UNHANDLED!!!!!!!!!!!");
				}


				break;
			}
		case irr::KEY_START:
			{
				this->GameSceneManager->getDynamicSceneObject("sceneObject.xml",  scene::ESO_CHARACTER);
				
				break;
			}

		default:
			break;
		}

		return result;
	}

	scene::IGameSceneManager* CBaseScene::getGameSceneManager()
	{
		return this->GameSceneManager;
	}

	void CBaseScene::addDynamicSceneObject(scene::IDynamicSceneObject* sceneObj)
	{
		this->DynamicObjects.push_back(sceneObj);
		
		if(this->Established)
		{
			sceneObj->addToScene();
			//this->GameSceneManager->getSceneManager()->addAnimatedMeshSceneNode(
		}
	}

	//	void CBaseScene::establishState()
	//	{
	//		//game::DeviceManage* a = game::DeviceManager::Instance;
	//		game::DeviceManager::getInstance().clearScreen();
	//		game::DeviceManager::getInstance().lock();
	//
	//		this->establishControls();
	//		this->establishScene();
	//
	//		game::DeviceManager::getInstance().unlock();
	//		
	//		Established = true;
	//	}
	//
	void CBaseScene::establishControls()
	{
	}

	//
	//	void CBaseScene::onNetworkManagerDisconneted()
	//	{
	//		
	//	}
	//
	//	bool CBaseScene::onServerRequest()
	//	{
	//		return false;
	//	}
}
